﻿using MCMesServer.DBFunctions.TransformFuncs;
using MesBLL.EventService;
using MesDAL.DBInfo;
using MesDAL.DTO.ProcessSheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.DBFuncs
{
    public class WorkPieceDBFunc
    {
        /// <summary>
        /// 添加工艺单具体信息到数据库内
        /// </summary>
        /// <param name="workpiece"></param>
        /// <returns></returns>
        public async static Task<WorkpieceDTO> AddWorkPieceIntoDB(WorkpieceDTO workpiece)
        {
            using (var ser = new WorkPieceInfoService())
            {
                workpiece_info select_info = await ser.SelectWorkpieceByNumberAndOrder(workpiece.number, workpiece.order);
                if (select_info is null)
                {
                    workpiece_info addnewpiece = TransformDTOFunc.TransformWorkPiece2Model(workpiece);
                    workpiece_info Info = await ser.AddAsync(addnewpiece);
                    WorkpieceDTO dto = TransformDTOFunc.TransformModel2WorkPieceDTO(Info);
                    return dto;
                }
                else
                {
                    WorkpieceDTO dto = TransformDTOFunc.TransformModel2WorkPieceDTO(select_info);
                    return dto;
                }
            }
        }

        /// <summary>
        /// 通过工件编号以及订单号查询工艺单信息
        /// </summary>
        /// <param name="number"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async static Task<WorkpieceDTO> SelectWorkPieceByNumberAndOrder(string number, string order)
        {
            using (var ser = new WorkPieceInfoService())
            {
                workpiece_info select_info = await ser.SelectWorkpieceByNumberAndOrder(number, order);
                if (select_info is null)
                {
                    return null;
                }
                else
                {
                    WorkpieceDTO dto = TransformDTOFunc.TransformModel2WorkPieceDTO(select_info);
                    return dto;
                }
            }
        }

        /// <summary>
        /// 通过工件id查询工件具体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async static Task<WorkpieceDTO> SelectWorkPieceByWorkpieceId(int id)
        {
            using (var ser = new WorkPieceInfoService())
            {
                workpiece_info select_info = await ser.SelectWorkpieceById(id);
                if (select_info is null)
                {
                    return null;
                }
                else
                {
                    WorkpieceDTO dto = TransformDTOFunc.TransformModel2WorkPieceDTO(select_info);
                    return dto;
                }
            }
        }

        /// <summary>
        /// 通过工艺单id查询工件信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async static Task<WorkpieceDTO> SelectWorkPieceByProcessId(int id)
        {
            using (var ser = new ProcessSheetService())
            {
                WorkpieceDTO select_info = await ser.SelectWorkpieceByProcessId(id);
                return select_info;
            }
        }
    }
}
